﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModelDinhMuc.Model;
namespace TinhNguyenVatLieuSanBay
{
    public partial class FrmChuyenBay : Form
    {
        DINHMUCSANBAYDB db = new DINHMUCSANBAYDB();
        public static FrmChuyenBay frmchuyenbay = null;
        IList<ChuyenBay> lstchuyenbay;
        bool themorcapnhat = false;
        public FrmChuyenBay()
        {
            InitializeComponent();
            frmchuyenbay = this;
            InitChuyenBay();
        }

        private void InitChuyenBay()
        {
            lstchuyenbay = db.SelectAllChuyenBay().ExecuteTypedList<ChuyenBay>();
            if (lstchuyenbay != null && lstchuyenbay.Count > 0)
            {
                chuyenBayBindingSource.DataSource = lstchuyenbay;
            }
        }

        private void InsertUpdateChuyenBay()
        {
            IList<ChuyenBay> lsttmp;
            string mamaybay=txtmamb.Text.ToUpper().Trim();
            string machuyenbay=txtmachuyenbay.Text.ToUpper().Trim();
            if (themorcapnhat == true)
            {
                foreach (var item in lstchuyenbay)
                {
                    if (item.MaMB == mamaybay && item.MaBAY == machuyenbay)
                    {
                        MessageBox.Show("Trùng mã máy bay hoặc mã chuyến bay!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtmamb.Focus();
                        return;
                    }
                }
            }
            else
            {
                int slphicong = Int32.Parse(txtsoluongphicong.Value.ToString());
                int sltiepvien = Int32.Parse(txtsoluongtiepvien.Value.ToString());
                lsttmp = db.InsertUpateChuyenBay(mamaybay, machuyenbay, slphicong, sltiepvien).ExecuteTypedList<ChuyenBay>();
                if (lsttmp != null && lsttmp.Count > 0)
                {
                    InitChuyenBay();
                    int count = lstchuyenbay.Count;
                    chuyenBayBindingSource.Position = count;
                }
            }
            themorcapnhat = false;
        }

        private void DeleteChuyenBay()
        {
            try
            {
                long id = long.Parse(gvchuyenbay.CurrentRow.Cells["iDDataGridViewTextBoxColumn"].Value.ToString());
                db.DeleteChuyenBay(id).Execute();
                InitChuyenBay();
            }
            catch
            {
            }
        }

        private void FrmChuyenBay_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmchuyenbay = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            themorcapnhat = true;
            txtmamb.Clear();
            txtmachuyenbay.Clear();
            txtmamb.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            InsertUpdateChuyenBay();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DeleteChuyenBay();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            IList<ChuyenBay> lsttmp;
            lsttmp = lstchuyenbay.Where(x => x.MaMB.Contains(txttimkiem.Text.ToUpper().Trim())).ToList();
            chuyenBayBindingSource.DataSource = lsttmp;
        }
    }
}
